/*
 * @Author: your name
 * @Date: 2020-05-12 14:32:44
 * @LastEditTime: 2020-05-26 12:00:04
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \tuyibo\servers\api\wbms\user.js
 */ 
var express = require('express');
var user_router = express.Router();
var db = require('../../models')
var auth = require('../../comm/auth')

//登录
user_router.post('/login', async (req, res) => {

  var result = { 'code': 500 }
  var name = req.body.username,
  txtPwd = req.body.pwd,
  txtVailCode = req.body.vailCode;
  let entext = auth.encryption(txtPwd)

  var querydata = await db.admin.findOne({where: {"name": name}});
  if( querydata != null && querydata.pwd ==  entext){
      req.session.regenerate(function(err) {
          if(err){
              return res.json(result)
          }

          result.code = 200;
          result.name = name
          result.token = auth.encryption(name)
          req.session.adminLoginUser = {"name":name, "role": querydata.role};
          res.json(result)
      });
  }else{
    res.json(result)
  }
});


user_router.post('/getUserList',async(req,res)=>{
  var list=await db.user.getUserList(req.body)
  res.json(list)
})


user_router.post('/logout', async (req, res)=>{
  auth.logout_admin_user(req)
  res.json({"code": 200})
});

module.exports = user_router